package com.student.controller;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.student.pojo.Result;
import com.student.pojo.Student;
import com.student.service.StudentService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
//@CrossOrigin
@RequestMapping("/student")
public class StudentController {
	@Resource
	StudentService studentService;
	@GetMapping
	public Result<?>findPage(@RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = "5") Integer pageSize,@RequestParam(defaultValue = "")String search){
		Page<Student> page=studentService.page(new Page<>(pageNum,pageSize),Wrappers.<Student>lambdaQuery().like(Student::getStudentname,search));
		return Result.success(200,"查询成功",page);
	}
	/*public Result<?>findAllStudent(){
		List<Student> studentList = studentService.list();
		return Result.success(200,"查询成功",studentList);
	}*/
	@PostMapping
	public Result<?>save(@RequestBody Student student){
		boolean save = studentService.save(student);
		if (save){
			return Result.success(200,"保存成功");
		}
		return Result.fail(1000,"保存失败");
	}
	@PutMapping
	public Result<?>update(@RequestBody Student student){
		if (studentService.updateById(student)) {
			return Result.success(200,"修改成功");
		}
		return Result.fail(1001,"修改失败");
	}
	@DeleteMapping("/{id}")
	public Result<?>delete(@PathVariable Integer id){
		if (studentService.removeById(id)) {
			return Result.success(200,"删除成功");
		}
		return Result.fail(1002,"删除失败");
	}
}
